﻿using System;
using System.Drawing;
using System.Windows;
using System.Windows.Input;
using Microsoft.Win32;
using vBridge.Design.Mvvm.Command;
using vBridge.Dwm.Aero.Margins;
using vBridge.Win32;

namespace vBridge.Wpf.Demo
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            UpdateBeepCommand();
        }

        #region Aero

        private readonly Random _random = new Random();

        private Int32 RandomInt32
        {
            get { return _random.Next(0, 100); }
        }

        private void OnFullGlassButtonClick(Object sender, RoutedEventArgs e)
        {
            AeroMargins = new MarginsViewModel(new Margin(-1));
        }

        private void OnNormalGlassButtonClick(Object sender, RoutedEventArgs e)
        {
            AeroMargins = new MarginsViewModel(new Margin(0));
        }

        private void OnRandomGlassButtonClick(Object sender, RoutedEventArgs e)
        {
            AeroMargins = new MarginsViewModel(new Margin(RandomInt32, RandomInt32, RandomInt32, RandomInt32));
        }

        #endregion

        #region Bitmap

        private Bitmap _bitmap;

        public Bitmap Bitmap
        {
            get { return _bitmap; }
            set
            {
                _bitmap = value;
                OnPropertyChanged();
            }
        }

        #region RelayCommand

        private RelayCommand _browseBitmap;

        public RelayCommand BrowseBitmap
        {
            get { return _browseBitmap ?? (_browseBitmap = new RelayCommand(GetBitmap)); }
        }

        #endregion

        private void GetBitmap()
        {
            var fileBrowser = new OpenFileDialog();
            fileBrowser.ShowDialog(this);
            String fileName = fileBrowser.FileName;

            if (fileName.IsNullOrWhitespace())
                return;

            try
            {
                Bitmap = new Bitmap(Image.FromFile(fileName));
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to load image. {0}: {1}", e.GetType(), e.Message);
            }
        }

        #endregion

        #region Beep (RelayCommandAsync)

        private ICommand _beepCommand;

        public ICommand BeepCommand
        {
            get { return _beepCommand; }
            private set
            {
                _beepCommand = value;
                OnPropertyChanged();
            } // TODO: Consider unnecessary notification.
        }

        private static void Beep(Double value)
        {
            Console.Beep((Int32) value, 2000);
        }

        private void AsyncToggleOnChecked(Object sender, RoutedEventArgs e)
        {
            UpdateBeepCommand();
        }

        private void AsyncToggleOnUnchecked(Object sender, RoutedEventArgs e)
        {
            UpdateBeepCommand();
        }

        private void UpdateBeepCommand()
        {
            BeepCommand =
                AsyncToggle.IsChecked == true
                    ? new RelayCommandAsync<Double>(Beep)
                    : new RelayCommand<Double>(Beep);
        }

        #endregion


    }
}